home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1994 November / macformat-018.iso / Demos / Extend 3.0 Demo / Demo Libraries / Demo Generic Lib / Demo Generic Lib.rsrc / MODL_7232_Accumulate < prev    next >
Encoding:
Text File  |  1994-06-22  |  5.1 KB  |  217 lines

  1. real    maxVal, lastReset;
  2. real    maxContents, maxTime;
  3. real    timeArray[];
  4. integer initFlag, resetCon;
  5. integer    discrete, newInput;
  6.  
  7. ** This block accumulates input values
  8. ** Copyright © 1989-1994 by Imagine That, Inc.
  9. ** All Rights Reserved.
  10. ** Extend Generic Library, Accumulate block; Alfy Riddle
  11. **            modified  3/1/92    JSL extensively modified for V2.0
  12. **                      12/8/92 JSL added (currentStep == 0 && !resetCon) 
  13. **                      2/26/93 JSL conOut only recalcs if not from plotter
  14. **                      7/20/93 JSL added if (!newInput) to on conOut
  15. //                       10/27/93 BD fixed animation level
  16. //                      2/14/94 DJK modified trace and report
  17. //                      3/10/94 DJK added block label to trace & report
  18. //                    5/18/94  BD changed dialog, help
  19.  
  20.  
  21. procedure calc()
  22. {
  23.     real    newVal;
  24.  
  25.             ** for initialization and resets
  26.     if (((currentStep == 0 && !resetCon) || resetIn > 0.5) && lastReset != currentTime)
  27.     {
  28.         if( initFlag )
  29.         {
  30.             if( noValue(initIn) )
  31.             {
  32.                 userError("BLANK value at Accumulate block s input.");
  33.                 abort;
  34.             }
  35.             accum = initIn; ** use connector for starting value if connected
  36.             startVal = accum;
  37.         }
  38.         else
  39.             accum = startVal;    ** use dialog box value
  40.  
  41.         lastReset = currentTime;
  42.     }
  43.     
  44.     if (!discrete || newInput)
  45.         newVal = ConIn;
  46.     else
  47.         newVal = 0.0;
  48.     newInput = FALSE;
  49.         
  50.         ** make sure valid data (blanks contaminate data)
  51.     if( !noValue(ConIn) )
  52.         {
  53.         accum = accum + newVal;
  54.  
  55.         if (accum > maxVal)        // increase maximum if necessary
  56.             maxVal = accum;
  57.         if (accum > maxContents)    // this will remember a smaller max level
  58.             maxContents = accum;    // remember max level from this run
  59.             
  60.         animationShow(1);
  61.         if (maxVal <= 0.0)
  62.             animationLevel(1, 0);
  63.         else
  64.             animationLevel(1, accum/ maxVal);
  65.         }
  66.  
  67.     conOut = accum;
  68.  
  69.     ** sysGlobal2 is the file reference number for the DEBUG TRACE
  70.     if( sysGlobal2 != 0.0 )    ** 0 is error, check for open file for TRACE
  71.         {
  72. // template for report:      |BLOCK NAME *****************|block number |BLOCK NUMBER*******
  73.         fileWrite(sysGlobal2,"Accumulator                  block number "+(MyBlockNumber())+".  Current Time:"+currentTime+".","",True);
  74.         if(getBlockLabel(myBlockNumber()) != "")
  75.             fileWrite(sysGlobal2,"Block Label: "+getBlockLabel(myBlockNumber()),"",True);
  76.         fileWrite(sysGlobal2,"     Input = "+conIn,"",True);
  77.         fileWrite(sysGlobal2,"     Contents = "+conOut,"",True);
  78.         fileWrite(sysGlobal2,"     Start Input = "+initIn,"",True);
  79.         fileWrite(sysGlobal2,"     Reset Input = "+resetIn,"",True);
  80.         fileWrite(sysGlobal2," ","",True);
  81.         }
  82. }
  83.  
  84.  
  85. // These connector messages are only called when this block is used in a 
  86. // Discrete Event Model.
  87.  
  88. on ConIn
  89. {
  90.     newInput = TRUE;
  91.     sendMsgToOutputs(initIn);
  92.     sendMsgToOutputs(resetIn);
  93.     calc();
  94.     sendMsgToInputs(conOut);
  95. }
  96.  
  97. // These connector messages are only called when this block is used in a 
  98. // Discrete Event Model.
  99.  
  100. on ConOut
  101. {
  102.     //  if the message is not coming from a plotter
  103.     if (sysGlobalint9 == 0)
  104.         {
  105.         if (!newInput)
  106.             sendMsgToOutputs(conIn);
  107.         sendMsgToOutputs(initIn);
  108.         sendMsgToOutputs(resetIn);
  109.         calc();
  110.         }
  111. }
  112.  
  113. // These connector messages are only called when this block is used in a 
  114. // Discrete Event Model.
  115.  
  116. on initIn
  117. {
  118.     sendMsgToOutputs(conIn);
  119.     sendMsgToOutputs(resetIn);
  120.     calc();
  121.     sendMsgToInputs(conOut);
  122. }
  123.  
  124. // These connector messages are only called when this block is used in a 
  125. // Discrete Event Model.
  126.  
  127. on resetIn
  128. {
  129.     lastReset = -1.0;
  130.     sendMsgToOutputs(conIn);
  131.     sendMsgToOutputs(initIn);
  132.     calc();
  133.     sendMsgToInputs(conOut);
  134. }
  135.  
  136.  
  137. ** This message occurs for each step in the simulation.
  138. on Simulate
  139. {
  140.     calc();
  141. }
  142.  
  143.  
  144. ** If the dialog data is inconsistent for simulation, abort.
  145. on checkData
  146. {    
  147.     sysGlobal1 = 0.0;    ** prevent false reports
  148.     sysGlobal2 = 0.0;    ** prevent false debugs
  149.  
  150.     resetCon = resetIn;
  151.  
  152.     if( initIn )
  153.         initFlag = 1;
  154.     else
  155.         initFlag = 0;
  156.     
  157.     if( noValue(startVal) AND !initFlag )
  158.         {
  159.         userError("Accumulate block needs a starting value");
  160.         abort;
  161.         }
  162. }
  163.  
  164.  
  165. ** Initialize any simulation variables.
  166. on initSim
  167. {
  168.     accum = startVal;
  169.  
  170.     maxVal = maxContents;    // start from max remembered from last run        
  171.     if (maxVal == 0.0)
  172.         maxVal = startVal;
  173.     
  174.     animationLevel(1, 0);
  175.     animationColor(1, 30000, 30000, 30000, 1);
  176.     if (animationOn)
  177.         animationShow(1);
  178.     else
  179.         animationHide(1, FALSE);
  180.  
  181.     maxTime        = endTime - startTime;
  182.     maxContents    = 0.0;
  183.     lastReset    = -1.0;
  184.  
  185.     discrete = FALSE;
  186.     if( getPassedArray(sysGlobal0, timeArray) )
  187.         {
  188.         discrete = TRUE;
  189.         getSimulateMsgs(FALSE);
  190.         }
  191. }
  192.  
  193.  
  194. on createBlock
  195. {
  196.     startVal = 0;
  197.     maxVal      = 0.0;
  198.     maxContents = 0.0;
  199. }
  200.  
  201.  
  202. on endSim
  203. {
  204.     ** sysGlobal1 is the file reference number for the TEXT REPORT
  205.     if( sysGlobal1 != 0.0 ) ** 0 is error, check for open file for REPORT
  206.         {
  207. // template for report:      |BLOCK NAME *****************|block number |BLOCK NUMBER*******
  208.         fileWrite(sysGlobal1,"Accumulator                  block number "+(MyBlockNumber()),"",True);
  209.         if(getBlockLabel(myBlockNumber()) != "")
  210.             fileWrite(sysGlobal1,"Block Label: "+getBlockLabel(myBlockNumber()),"",True);
  211.         fileWrite(sysGlobal1,"     Starting Contents = "+startVal,"",True);
  212.         fileWrite(sysGlobal1,"     Final Contents = "+accum,"",True);
  213.         if( comments != "" )
  214.             fileWrite(sysGlobal1,"     Comments = "+comments,"",True);        
  215.         fileWrite(sysGlobal1," ","",True);
  216.         }
  217. }